// handling optionals

import net.http

// propagate the error
//     http.get returns ?http.Response
//     ? follows the call, the error will be propagated to the caller of f
//     when using ? after a function call producing an optional
//     the enclosing function must return an optional
fn f(url string) ?string {
	resp := http.get(url)?
	// resp := http.get(url) or { return err }
	return resp.text
}

// break from execution early
user := repo.find_user_by_id(7) or { return }

// provide a default value at the end of `or` block
fn do_something(s string) ?string {
	if s == 'foo' {
		return 'foo'
	}
	return error('invalid string') // Could be `return none` as well
}

a := do_something('foo') or { 'default' } // a will be 'foo'
b := do_something('bar') or { 'default' } // b will be 'default'
println(a)
println(b)

// use `if` unwrapping
//     `resp` is only in scope for the first `if` branch
//     `err` is only in scope for the `else` branch
if resp := http.get('https://google.com') {
	println(resp.text) // resp is a http.Response, not an optional
} else {
	println(err)
}
